(function () {
    var SeatPosition = [
        {x : 230, y : 35},   //上
        {x : 590, y : 655},  //右
        {x : 225, y : 1045}, //下
        {x : 15,  y : 655},  //左
    ];

    /**
     * 用户座位扩展方法
     * @class SeatUI
     * @constructor
     * 
     * @param {Number}   SeatID        座位ID
     * @param {Function} ClickCallBack 点击用户头像时的回调函数
     */
    function SeatUI(SeatID, ClickCallBack) {
        SeatUI.super(this);

        this._seatID = SeatID;
        this._position = SeatID;
        this._player = null;
        this._status = null;
        this._coin = null;
        this._chatUI = null;
        this._clickHandler = null;
        this._isDealCard = false;
        this._isReadyCompare = false;

        this._cards = [];
        this._firstCards = [];
        this._secondCards = [];
        this._thirdCards = [];

        this.x = 237;
        this.y = 522;

        this.name = "Seat." + SeatID;

        this.CardType.zOrder = 50;
        this.SpecialCard.zOrder = 50;
        this.SortingCards.zOrder = 50;

        setTimeout(function () {
            this.changePosition(SeatID);
        }.bind(this), 0);

        //为用户头像绑定点击事件
        this.avatarPanel.on(Laya.Event.CLICK, this, function () {
            this._clickHandler && this._clickHandler.run();
        });

        this.firstCardPanel.zOrder = 1;
        this.secondCardPanel.zOrder = 2;
        this.thirdCardPanel.zOrder = 3;
    }
    Laya.class(SeatUI, "ui.Com.Seat", ui.Room.UI.SeatUI);

    var _proto = SeatUI.prototype;
    /**
     * @privite
     * 改变用户当前的游戏状态
     * 
     * @param {String} status 用户游戏状态
     */
    _proto.changeStatus = function (status) {
        this._status = status;
        switch(this._status){
            case "ready":
                this.userReady.visible = true;
                break;
            case "waiting":
            case "empty":
                this.userReady.visible = false;
                this.reset();
                break;
            case "cancel":
            case "gaming":
            default:
                this.userReady.visible = false;
                break;
        }
    }
    /**
     * @privite
     * 改变或获取用户座位号
     * 
     * @param {Number} position 用户座位号
     */
    _proto.changePosition = function (position) {
        var positionIndex = (position | 0) - 1;

        this._position = position;

        this.adjustment();

        Laya.Tween.to(this, {x : SeatPosition[positionIndex].x, y : SeatPosition[positionIndex].y}, 200);
    }
    /**
     * @privite
     * 调整用户座位状态
     */
    _proto.adjustment = function () {
        this.userReady.x = 43;
        this.userReady.y = -132;

        this.CardPanel.x = 0;
        this.CardPanel.y = -18;

        this.InfoPanel.x = 127;
        this.InfoPanel.y = 27;

        //this.CardType.visible = true;

        switch(this._position){
            case 1:
                //调整1号座的已准备手势位置
                this.userReady.x = 115;
                this.userReady.y = 165;
                //调整1号座的扑克牌盒子位置
                this.CardPanel.x = -11;
                this.CardPanel.y = 305;
                //调整1号座的特殊牌型文字位置
                this.SpecialCard.x = 50;
                this.SpecialCard.y = 235;
                //调整1号座的比牌文字位置
                this.CardType.x = 155;
                this.CardType.y = 200;
                //调整1号座的理牌中文字位置
                this.SortingCards.x = 70;
                this.SortingCards.y = 225;
                break;
            case 2:
                //调整2号座的用户信息位置
                this.InfoPanel.x = -185;
                //调整2号座的扑克牌盒子位置
                this.CardPanel.x = -200;
                //调整2号座的特殊牌型文字位置
                this.SpecialCard.x = -110;
                this.SpecialCard.y = -140;
                //调整2号座的比牌文字位置
                this.CardType.x = 0;
                this.CardType.y = -180;
                //调整2号座的理牌中文字位置
                this.SortingCards.x = -120;
                this.SortingCards.y = -104;
                break;
            case 3:
                //调整3号座的已准备手势位置
                this.userReady.x = 115;
                //调整3号座的扑克牌盒子位置
                this.CardPanel.x = -11;
                //调整3号座的特殊牌型文字位置
                this.SpecialCard.x = 70;
                this.SpecialCard.y = -140;
                //调整3号座的比牌文字位置
                this.CardType.x = 175;
                this.CardType.y = -180;
                //调整3号座的理牌中文字位置
                this.SortingCards.x = 70;
                this.SortingCards.y = -104;
                break;
            case 4:
                //调整4号座的扑克牌盒子位置
                this.CardPanel.x = 25;
                //调整4号座的特殊牌型文字位置
                this.SpecialCard.x = 55;
                this.SpecialCard.y = -140;
                //调整4号座的比牌文字位置
                this.CardType.x = 160;
                this.CardType.y = -180;
                //调整4号座的理牌中文字位置
                this.SortingCards.x = 70;
                this.SortingCards.y = -104;
                break;
        }

    }
    /**
     * @privite
     * 恢复座位原始状态
     */
    _proto.reset = function () {
        this._isDealCard = false;
        this._isReadyCompare = false;

        this._cards = [];
        this._firstCards = [];
        this._secondCards = [];
        this._thirdCards = [];

        this.winAmount.y = 60;
        this.winAmount.text = "";
        this.winAmount.visible = false;

        this.resultGun.rotation = 0;
        this.resultGun.visible = false;

        this.resultBullet.removeChildren();
        
        this.firstCardPanel.removeChildren();
        this.secondCardPanel.removeChildren();
        this.thirdCardPanel.removeChildren();
        this.firstCardPanel.zOrder = 1;
        this.secondCardPanel.zOrder = 2;
        this.thirdCardPanel.zOrder = 3;

        this.SortingCards.visible = false;
        this.CardType.visible = false;
        this.SpecialCard.visible = false;
    }
    /**
     * @privite
     * 比牌
     */
    _proto.compareLine = function (lineCards, cardsinfo, posY, lineFlag, cardPanel) {
        if(this._status !== "gaming"){
            return;
        }
        this.CardType.visible = true;
        this.CardType.y = posY;
        this.CardType.changeText(cardsinfo.name);
        var size = lineCards.length;
        var startAngle = -10;
        if(size !== 3){
            startAngle = -20;
        }

        cardPanel.zOrder += 3;
        Laya.SoundManager.playSound("sound/" + cardsinfo.type + ".mp3");

        for(var i in lineCards){
            var _card = lineCards[i];
            _card.point = cardsinfo.cards[i];
            _card.rotation = startAngle;
            _card.scale(1, 1);
            // _card.zOrder = (i | 0) + lineFlag + 14;
            _card.halfPass();
            Laya.timer.once(1000, _card, function () {
                this.scale(0.8, 0.8);
                this.rotation = 0;
            });

            startAngle += 10;
        }

        Laya.timer.once(1000, this, function () {
            this.CardType.visible = false;
        });
    }
    /**
     * @privite
     * 打枪被击中
     */
    _proto.hit = function () {
        var bullet = this.resultBullet;
        Laya.SoundManager.playSound("sound/gun_fire.mp3");
        for(var i = 0; i < 3; i++){
            (function (i, bullet) {
                setTimeout(function () {
                    var hole = new Laya.Sprite();
                    hole.loadImage("res/seat/hole.png");
                    hole.anchorX = 0.5;
                    hole.anchorY = 0.5;
                    hole.x = (138 - 50) * Math.random();
                    hole.y = (138 - 50) * Math.random();
                    bullet.addChild(hole);
                }, i * 200);
            })(i, bullet);
        }
    }

    /**
     * @public
     * 聊天气泡
     * 
     * @param {String} type    聊天类型，可选text,emotion
     * @param {Object} content 聊天相关数据
     */
    _proto.chat = function (type, content) {
        if(this._chatUI !== null){
            Laya.Tween.clearAll(this._chatUI);
            this.removeChild(this._chatUI);
            this._chatUI = null;
        }
        var chatTip = this._chatUI = new ui.Room.UI.ChatUI();
        chatTip.alpha = 0;
        
        switch(type){
            case "text":
                chatTip.message.text = content.content;
                if(chatTip.message.width >= 315){
                    chatTip.message.wordWrap = true;
                    chatTip.message.width = 315;
                }

                chatTip.width  = chatTip.message.width + 30;
                chatTip.height = chatTip.message.height + 30;
                if(chatTip.width < 100){
                    chatTip.width = 100;
                }
                break;
            case "emotion":
                chatTip.message.visible = false;
                var emotion = new Laya.Image();

                emotion.width  = 80;
                emotion.height = 80;
                emotion.centerX = 0;
                emotion.centerY = 0;
                emotion.loadImage(content.img, 0, 0, 80, 80, Laya.Handler.create(emotion, function (emoImg) {
                    this.skin = emoImg;
                }, [content.img]));

                chatTip.addChild(emotion);
                break;
        }

        switch(this._position){
            case 1:
                chatTip.left = 20;
                chatTip.top = 170;
                chatTip.arrowUp.visible = true;
                break;
            case 2:
                switch (type) {
                    case "text":
                        chatTip.anchorX = 1;
                        chatTip.x = this.width - 20;
                        chatTip.arrowDown.left = NaN;
                        chatTip.arrowDown.right = 30;
                        break;
                    case "emotion":
                        chatTip.right = 20;
                        break;
                }
                
                chatTip.bottom = 170;
                chatTip.arrowDown.visible = true;
                break;
            case 3:
                chatTip.left = 20;
                chatTip.bottom = 170;
                chatTip.arrowDown.visible = true;
                break;
            case 4:
                chatTip.left = 20;
                chatTip.bottom = 170;
                chatTip.arrowDown.visible = true;
                break;
        }

        this.addChild(chatTip);
        Laya.Tween.to(chatTip, {alpha : 1}, 1000, Laya.Ease.backOut, Laya.Handler.create(chatTip, function () {
            Laya.timer.once(2000, this, function () {
                Laya.Tween.to(chatTip, {alpha : 0}, 1000, Laya.Ease.backIn, Laya.Handler.create(chatTip, function () {
                    this.removeSelf();
                }));
            });
        }));
    }
    /**
     * @public
     * 向该座位的用户发牌
     */
    _proto.dealCards = function (animationFlag) {
        if(this._status !== "gaming"){
            return;
        }
        if(this._isDealCard){
            utils.log("Seat:" + this._seatID + "重复发牌");
            return;
        }
        this._isDealCard = true;

        var CardsUI = this._cards;
        var CardPanel = this.CardPanel;

        for(var i = 0; i < 13; i++){
            var _card = new ui.Com.Card();
            var diff  = 0;
            if(this._position == 4){
                diff = -40;
            }

            _card.name = "Card." + i;
            _card.x = 20 + 40 + diff;
            _card.y = 0;

            _card.anchorX = 0.5;
            _card.anchorY = 1;

            _card.scaleX = 0.8;
            _card.scaleY = 0.8;

            _card.zOrder = i;

            CardPanel.addChild(_card);
            CardsUI.push(_card);

            if(animationFlag !== false)
                Laya.Tween.to(_card, {x : i * 20 + 20 + 40 + diff}, 800, Laya.Ease.bounceOut);
            else
                _card.x = i * 20 + 20 + 40 + diff;
        }

        this.SortingCards.visible = true;
    }
    /**
     * @public
     * 准备比牌，调整扑克牌的位置
     */
    _proto.readyCompare = function (specialFlag, cards) {
        if(this._status !== "gaming"){
            return;
        }
        if(this._isReadyCompare){
            utils.log("Seat:" + this._seatID + "重复准备比牌");
            return;
        }
        this._isReadyCompare = true;

        var CardsUI = this._cards;
        var FirstCards = this._firstCards;
        var SecondCards = this._secondCards;
        var ThirdCards = this._thirdCards;
        var position = this._position;

        for(var i = 0; i < 13; i ++){
            var card = CardsUI[i];
            if(cards){
                card.point = cards[i];
                card.halfPass();
            }
            
            if(i < 3){
                card.x = i * 40 + 15 + 42 + 40;
            }else{
                var xindex = (i - 3) % 5;
                var yindex = (i + 2) / 5 | 0;
                card.x = xindex * 40 + 15 + 40;
                card.y = yindex * 55;
            }

            switch(position){
                case 1:
                    card.x += 30;
                    card.y -= 35;
                    break;
                case 2:
                    card.x += 60;
                    card.y -= 90;
                    break;
                case 3:
                    card.x += 50;
                    card.y -= 90;
                    break;
                case 4:
                    card.y -= 90;
                    break;

            }

            card.removeSelf();
            if(i < 3){
                this.firstCardPanel.addChild(card);
                FirstCards.push(card);
            }else if(i >= 3 && i <8){
                this.secondCardPanel.addChild(card);
                SecondCards.push(card);
            }else{
                this.thirdCardPanel.addChild(card);
                ThirdCards.push(card);
            }
        }

        this.SortingCards.visible = false;
        if(specialFlag){
            this.SpecialCard.visible = true;
        }

        // switch(position){
        //     case 2:
        //         this.CardType.x = 30;
        //         break;
        //     case 4:
        //         this.CardType.x = -50;
        //         break;
        // }
    }
    /**
     * @public
     * 比牌动画
     * 
     * @param {String}   line 当前行标识，可选first,second,third
     * @param {Object}   cardsinfo 当前行的扑克牌数据
     * @param {Functino} callback 比牌动画执行完的回调函数
     */
    _proto.compare = function (line, cardsinfo, callback) {
        var lineCards = null;
        var posY = null;
        var lineFlag = null;
        var cardPanel = null;

        switch(line){
            case "first":
                posY = -180;
                if(this._position == 1){
                    posY = 200;
                }
                lineCards = this._firstCards;
                lineFlag = 1;
                cardPanel = this.firstCardPanel;

                utils.log("头道：" + cardsinfo.name);
                break;
            case "second":
                posY = -120;
                if(this._position == 1){
                    posY = 250;
                }
                lineCards = this._secondCards;
                lineFlag = 2;
                cardPanel = this.secondCardPanel;

                utils.log("中道：" + cardsinfo.name);
                break;
            case "third":
                posY = -70;
                if(this._position == 1){
                    posY = 310;
                }
                lineCards = this._thirdCards;
                lineFlag = 3;
                cardPanel = this.thirdCardPanel;

                utils.log("尾道：" + cardsinfo.name);
                break;
        }

        this.compareLine(lineCards, cardsinfo, posY, lineFlag, cardPanel);

        if(callback && typeof callback === "function"){
            setTimeout(callback, 1100);
        }
    }
    /**
     * @public
     * 用户入座
     * 
     * @param {Object} PlayerInfo 用户信息
     */
    _proto.sitdown = function (PlayerInfo) {
        this.avatarPanel.visible = true;
        this.InfoPanel.visible = true;

        this.userName.text = (PlayerInfo.uname || PlayerInfo.uid);
        this.gameScore.text = (PlayerInfo.amount || 0);

        this.userAvatar.loadImage(PlayerInfo.avatar, 0, 0, 104, 104, Laya.Handler.create(this.userAvatar, function (avatar) {
            utils.log("更换头像:" + avatar);
            this.skin = avatar;
        }, [PlayerInfo.avatar]));

        this._player = PlayerInfo;
    }
    /**
     * @public
     * 用户离座，清空座位信息
     */
    _proto.situp = function () {
        this.avatarPanel.visible = false;
        this.InfoPanel.visible = false;

        this.userName.text = "虚位以待";
        this.gameScore.text = 0;
        this.userAvatar.skin = "";
        this.status = "empty";

        this._player = null;
        this._coin = null;

        this.reset();
    }
    /**
     * @public
     * 打枪，旋转枪指向指定座位
     * 
     * @param {ui.Com.Seat} Seat     玩家座位
     * @param {Function}    callback 打枪动画执行完的回调函数
     */
    _proto.shoot = function (Seat, callback) {
        var angle = utils.angle(this.x, -this.y, Seat.x, -Seat.y);

        this.resultGun.rotation = angle;
        this.resultGun.visible = true;
        Laya.SoundManager.playSound("sound/gun_speak.mp3");

        Seat.hit();
        utils.log("shoot----->>");
        utils.log(this.x, this.y, Seat.x, Seat.y);
        utils.log(this.position, Seat.position, angle);

        if(callback && typeof callback === "function"){
            setTimeout(function () {
                this.resultGun.rotation = -90;
                this.resultGun.visible = false;
                callback();
            }.bind(this), 1000);
        }
    }
    /**
     * @public
     * 结算动画
     */
    _proto.result = function (amount) {
        this.firstCardPanel.removeChildren();
        this.secondCardPanel.removeChildren();
        this.thirdCardPanel.removeChildren();
        
        var winAmountUI = this.winAmount;
        if(amount >= 0){
            winAmountUI.font = "font_win";
            winAmountUI.text = "+" + amount;
        }else{
            winAmountUI.font = "font_lose";
            winAmountUI.text = amount;
        }
        winAmountUI.visible = true;
        Laya.Tween.to(winAmountUI, {y : -30}, 300, Laya.Ease.cubicOut);
    }

    /**
     * @public
     * 改变或获取用户游戏状态
     * @property {String} status 用户游戏状态
     * 
     * {
     *     empty   : 空座位
     *     cancel  : 取消准备
     *     ready   : 已准备
     *     gaming  : 正在游戏中
     *     compare : 比牌阶段
     * }
     */
    Laya.getset(0, _proto, "status", function () {
        return this._status;
    }, function (status) {
        this.changeStatus(status);
    });
    /**
     * @public
     * 改变或获取用户座位号
     * @property {Number} position 用户座位号
     * 
     * {
     *     1 : {x : 230, y : 35},   //上
     *     2 : {x : 590, y : 655},  //右
     *     3 : {x : 225, y : 1045}, //下
     *     4 : {x : 15,  y : 655},  //左
     * }
     */
    Laya.getset(0, _proto, "position", function () {
        return this._position;
    }, function (position) {
        this.changePosition(position);
    });
    /**
     * @public
     * 改变或获取用户座位上的游戏币
     * @property {Number} coin 用户游戏币
     */
    Laya.getset(0, _proto, "coin", function () {
        return this._coin;
    }, function (coin) {
        this._coin = coin;
        this.gameScore.text = coin;
    });
    /**
     * @public
     * 改变或获取点击用户头像事件
     * @property {Number} clickHandler 点击用户头像的回调
     */
    Laya.getset(0, _proto, "clickHandler", function () {
        return this._clickHandler;
    }, function (value) {
        this._clickHandler = value;
    });
    /**
     * @public
     * 获取当前座位的用户信息
     * @property {Object} player 用户信息
     */
    Laya.getset(0, _proto, "player", function () {
        return this._player;
    }, function (value) {
        this._player = value;
    });
})();